/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.donto.telas;

import javax.swing.JOptionPane;

import org.jfree.chart.*;

import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.axis.*;
import org.jfree.data.xy.*;


import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.general.DefaultPieDataset;
        
 /**
 *
 * @author Cliente Master
 */
public class telaGraficos extends javax.swing.JFrame {

    
    /**
     * Creates new form telaGraficos
     */
    public telaGraficos() {
        initComponents();
        setLocationRelativeTo(null);
        
        
        this.panelLinhas.setVisible(false);
        
       this.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        linha = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDados = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        Painel = new javax.swing.JLayeredPane();
        panelLinhas = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        linha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        linha.setText("Linha");
        linha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linhaActionPerformed(evt);
            }
        });

        tbDados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "X", "Y"
            }
        ));
        jScrollPane1.setViewportView(tbDados);

        jButton1.setText("Desenhar Gráfico");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        Painel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout panelLinhasLayout = new javax.swing.GroupLayout(panelLinhas);
        panelLinhas.setLayout(panelLinhasLayout);
        panelLinhasLayout.setHorizontalGroup(
            panelLinhasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 431, Short.MAX_VALUE)
        );
        panelLinhasLayout.setVerticalGroup(
            panelLinhasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 472, Short.MAX_VALUE)
        );

        Painel.add(panelLinhas);
        panelLinhas.setBounds(20, 10, 431, 472);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(linha)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Painel, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(linha)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(0, 312, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Painel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void linhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_linhaActionPerformed
        panelLinhas.setVisible(true);
        
        Painel.setLayer(panelLinhas, 0, 0);
    }//GEN-LAST:event_linhaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ChartPanel panel;
        JFreeChart chart = null;
               
        if(linha.isSelected()){ //GRAFICO DE LINHAS
            int validar=1;
        XYSplineRenderer renderer = new XYSplineRenderer();
        XYSeriesCollection dataset = new XYSeriesCollection();
        
        ValueAxis x = new NumberAxis();
        ValueAxis y = new NumberAxis();
        
        XYSeries serie = new XYSeries("Dados");
        
        XYPlot plot;
        linha.removeAll();
        
        try{
            for(int fila=0;fila<3;fila++){
                serie.add(Float.parseFloat(String.valueOf(tbDados.getValueAt(fila,0))),
                        Float.parseFloat(String.valueOf(tbDados.getValueAt(fila,1))));
            }
        }catch(Exception ex){
            validar=0;
        }
        if(validar==1){
            dataset.addSeries(serie);
        
        x.setLabel("Eje X");
        y.setLabel("Eje Y");
        plot = new XYPlot(dataset,x,y,renderer);
        chart = new JFreeChart(plot);
        chart.setTitle("Gráfico de Linhas");
        }else{
            JOptionPane.showMessageDialog(this, "Deve informar na tabela os valores em números");
        }
        
        
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane Painel;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton linha;
    private javax.swing.JPanel panelLinhas;
    private javax.swing.JTable tbDados;
    // End of variables declaration//GEN-END:variables
}
